package com.ruoyi.mapper;

import java.util.Date;
import java.util.List;
import com.ruoyi.domain.LendMessage;
import com.ruoyi.domain.dto.EmailDto;
import com.ruoyi.domain.dto.HotListDto;
import com.ruoyi.domain.dto.LendMessageDto;
import com.ruoyi.domain.dto.UserLendDto;
import org.apache.ibatis.annotations.*;

/**
 * 图书借阅Mapper接口
 *
 * @author qyb
 * @date 2023-12-26
 */
@Mapper
public interface LendMessageMapper
{
    /**
     * 查询图书借阅
     *
     * @param lendId 图书借阅主键
     * @return 图书借阅
     */
    public LendMessage selectLendMessageByLendId(Long lendId);

    /**
     * 查询图书借阅列表
     *
     * @param lendMessage 图书借阅
     * @return 图书借阅集合
     */
    public List<LendMessage> selectLendMessageList(LendMessage lendMessage);

    /**
     * 多功能查询图书借阅
     *
     * @param lendMessageDto 图书借阅
     * @return 图书借阅集合
     */
    List<LendMessageDto> selectLendMessage(LendMessageDto lendMessageDto);

    /**
     * 查询用户借了几本书
     *
     * @param userId 用户id
     * @return 图书借阅集合
     */
    public UserLendDto selectLendMessageByUserId(Long userId);

    /**
     * 查询借书热榜
     *
     * @param lendMessageDto 图书借阅
     * @return 图书借阅集合
     */
    public List<HotListDto> selectHotList(LendMessageDto lendMessageDto);


    /**
     * 逻辑删除借阅
     *
     * @param lendMessage 图书借阅
     * @return 结果
     */
    public int updateIsDelete(LendMessage lendMessage);


    /**
     * 新增图书借阅
     *
     * @param lendMessage 图书借阅
     * @return 结果
     */
    public int insertLendMessage(LendMessage lendMessage);

    /**
     * 修改图书借阅
     *
     * @param lendMessage 图书借阅
     * @return 结果
     */
    public int updateLendMessage(LendMessage lendMessage);

    /**
     * 删除图书借阅
     *
     * @param lendId 图书借阅主键
     * @return 结果
     */
    public int deleteLendMessageByLendId(Long lendId);

    /**
     * 批量删除图书借阅
     *
     * @param lendIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteLendMessageByLendIds(Long[] lendIds);

    /**
     *选择即将到期图书
     *
     * @return 结果
     */
    public List<EmailDto> selectDueBookByDay(int lendExpiring);

    /**
     *选择到期60天图书图书
     *
     * @return 结果
     */
    public List<EmailDto> selectExpireBookByDay(int penaltyDay);

    @Select("SELECT count(*) from sys_lend_list WHERE lend_date BETWEEN #{beginTime} AND #{endTime} AND is_delete = 0 ")
    Integer getLendEachMonth(@Param("beginTime")String beginTime, @Param("endTime") String endTime);



    @Select("SELECT count(*) FROM sys_lend_list WHERE lend_date = #{date}")
    int getLendEachDay(Date date);



    //根据类型和时间查询是否存在该条记录
    @Select("SELECT count(*) FROM sys_count WHERE type = #{type} AND create_time = #{createTime}")
    int countByTypeAndCreateTime(@Param("type") int type, @Param("createTime") String createTime);

    //如果存在，就查询数量
    @Select("SELECT number FROM sys_count WHERE type = #{type} AND create_time = #{createTime}")
    int countNumber(@Param("type") int type, @Param("createTime") String createTime);


    //如果数量不一致就更新数量
    @Update("update sys_count set number = #{lendCount} where type = #{type} and create_time = #{createTime}")
    int updateNumber (@Param("type") int type, @Param("createTime") String createTime, @Param("lendCount") int lendCount);

    //将统计数据持久化保存到count表中去
    @Insert("insert into sys_count (type,create_time,number) VALUES (#{i},#{date},#{lendCount})")
    int insertIntoTbaleCount(@Param("i") int i, @Param("date") String date, @Param("lendCount") int lendCount);

    @Select("SELECT  DISTINCT \n" +
            "\tsys_book_author_dict.author \n" +
            "FROM\n" +
            "\tsys_book_author_dict,\n" +
            "\tsys_lend_list,\n" +
            "\tsys_book \n" +
            "WHERE\n" +
            "\tsys_lend_list.book_id = sys_book.book_id \n" +
            "\tAND sys_book.author = sys_book_author_dict.author_value")
    List<String> selectAllAuthor();

    @Select("\t\n" +
            "\tSELECT author_value FROM sys_book_author_dict WHERE author = #{author}")
    Integer selectAuthorId(String author);


    @Select("\n" +
            "\tSELECT count(*) FROM sys_lend_list,sys_book WHERE sys_lend_list.book_id = sys_book.book_id AND sys_book.author = #{id}")
    Integer selectCount(int id);


    @Select("select count(*) from sys_lend_list")
    Integer selectAllNumber();


    @Select("\t\n" +
            "select count(*) from sys_lend_list WHERE back_date > should_back_time")
    Integer selectBackLaterCount();
}
